package com.gw2.gw2world.web.server;

import org.hibernate.*;
import org.hibernate.cfg.*;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import com.gw2.gw2world.web.server.model.*;

public class HibernateUtils {
    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry;

    private static SessionFactory configureSessionFactory() throws HibernateException {
        Configuration configuration = new Configuration();
        configuration.configure("hibernate.cfg.xml");
        configuration.addAnnotatedClass(Server.class);
        configuration.addAnnotatedClass(Event.class);
        configuration.addAnnotatedClass(Guild.class);
        configuration.addAnnotatedClass(Item.class);
        //configuration.addAnnotatedClass(MetaEvent.class);
        configuration.addAnnotatedClass(MiniMap.class);
        configuration.addAnnotatedClass(Recipe.class);
        configuration.addAnnotatedClass(Translation.class);
        configuration.addAnnotatedClass(User.class);
        serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        return sessionFactory;
    }

    public static SessionFactory getSessionFactory() {return configureSessionFactory();}
    
    public static void shutdown() {getSessionFactory().close();}
}
